<?php namespace App\Admin\Enterprise\Personnel\Criterias;

use Input;
use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
//基础条件
use App\Core\Repository\Criterias\NotDeleteCriteria;
use App\Core\Repository\Criterias\FixedCriteria;
//Cabinet
use App\Admin\Enterprise\Enterprise\Repository as EnterpriseRepository;

class MatchCabinet extends Criteria{

    public function apply(Repository $repository){
        parent::apply($repository);
        $enterpriseId = Input::get('enterprise_id');
        if(!$enterpriseId || !is_numeric($enterpriseId)){
            return $this->query->whereRaw('1=0');
        }
        $this->enterpriseId = $enterpriseId;
        $rep = new EnterpriseRepository();
        $rep->addCriteria(new NotDeleteCriteria());
        $rep->addCriteria(new FixedCriteria('id', $enterpriseId));
        $enterprise = $rep->first();
        if($enterprise){
            $field = $this->formatField($this->model, 'enterprise_id');
            $this->query->where($field, '=', $enterprise->id);
            return ;
        }
        $this->query->whereRaw('1=0');
    }
}